﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class SequenceNodeHandler : CompositeNodeHandler
{
    public override BehaviorNodeState HandleComposite(BehaviorEntity entity, MultiBranchNodeData node)
    {
        if (node.CurIndex < node.ChildNodes.Count)
        {
            GetChildNodeHandler(node.ChildNodes[node.CurIndex].CurNodeType).ExcuteNode(entity, node.ChildNodes[node.CurIndex]);

            if (node.ChildNodes[node.CurIndex].CurNodeState == BehaviorNodeState.Success)
            {
                node.CurIndex++;
                node.CurNodeState = BehaviorNodeState.Running;
                if (node.CurIndex >= node.ChildNodes.Count)
                {
                    node.CurNodeState = BehaviorNodeState.Success;
                }
            }
            else if (node.ChildNodes[node.CurIndex].CurNodeState == BehaviorNodeState.Failure)
            {
                node.CurIndex = node.ChildNodes.Count;
                node.CurNodeState = BehaviorNodeState.Failure;
            }
            else if (node.ChildNodes[node.CurIndex].CurNodeState == BehaviorNodeState.Invalid)
            {
                node.CurIndex++;
                node.CurNodeState = BehaviorNodeState.Running;
            }
            else if (node.ChildNodes[node.CurIndex].CurNodeState == BehaviorNodeState.Running)
            {
                node.CurNodeState = BehaviorNodeState.Running;
            }
            else
            {
                node.CurNodeState = BehaviorNodeState.Success;
            }
        }
        return node.CurNodeState;
    }
}